import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart' hide Router;
import 'package:mi_mang_flutter/page/home/home_page.dart';
import 'package:mi_mang_flutter/page/home/home_router.dart';
import 'package:mi_mang_flutter/page/login/login_router.dart';
import 'package:mi_mang_flutter/page/public/webview_page.dart';
import 'package:mi_mang_flutter/page/setting/personal_info/personal_info_router.dart';
import 'package:mi_mang_flutter/page/setting/setting_router.dart';
import 'package:mi_mang_flutter/routers/router_init.dart';
import '../widget/widget_not_found.dart';

class Routes {
  static String main = "/home";
  static String webViewPage = "/webview";

  static List<IRouterProvider> _listRouter = [];

  static void configureRoutes(FluroRouter router) {
    /// 指定路由跳转错误返回页
    router.notFoundHandler = Handler(
      handlerFunc: (context, params) {
        debugPrint("未找到目标页");
        return WidgetNotFound();
      },
    );

    /// [selectedIndex] 选中的tab项索引
    router.define(
      main,
      handler: Handler(handlerFunc: (context, params) => HomePage()),
    );

    router.define(
      webViewPage,
      handler: Handler(
        handlerFunc: (_, params) {
          String? title = params['title']?.first;
          String? url = params['url']?.first;
          return WebViewPage(title: title, url: url);
        },
      ),
    );

    _listRouter.clear();

    /// 各自路由由各自模块管理，统一在此添加初始化
    _listRouter.add(LoginRouter());
    _listRouter.add(SettingRouter());
    _listRouter.add(HomeRouter());
    _listRouter.add(PersonalInfoRouter());

    /// 初始化路由
    _listRouter.forEach((routerProvider) {
      routerProvider.initRouter(router);
    });
  }
}
